package com.atguigu.ucenterservice.service.impl;

import com.atguigu.common.exception.GuliException;
import com.atguigu.common.utils.JwtUtils;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.register.RegisterVo;
import com.atguigu.ucenterservice.mapper.UcenterMemberMapper;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import com.atguigu.ucenterservice.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContextExtensionsKt;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-04-20
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录查询，返回token字符串
    @Override

    public String login(UcenterMember member) {
        //1 从传递对象参数获取需要两个值
        String mobile = member.getMobile();
        String password = member.getPassword();

        //2 判断对象手机号和密码是否为空
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) ) {
            throw new GuliException(20001,"登录失败");
        }

        //3 根据手机号查询数据
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        //判断：如果根据手机号查询数据为空
        if(ucenterMember == null) { //表里面没有手机号数据
            throw new GuliException(20001,"登录失败");
        }

        //4 校验手机号对应的密码是否正确
        //把输入的密码进行MD5加密，和数据库已经加密存储进行比较
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())) {
            throw new GuliException(20001,"登录失败");
        }



        //6 根据用户信息生成token字符串
        //使用通过手机号查询出来对象，获取id和昵称生成token字符串
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());

        return jwtToken;
    }

    //注册
    @Override


    public void register(RegisterVo registerVo) {
        //1 获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        //手机验证码
        String code = registerVo.getCode();

        //2 判断值是否为空
        if(StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(nickname) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new GuliException(20001,"注册失败");
        }
//
//        //3 判断验证码是否正确
//        //从redis获取发送验证码，和输入的验证码比较
//        String redisCode = redisTemplate.opsForValue().get(mobile);
//        if(!code.equals(redisCode)) {
//            throw new GuliException(20001,"注册失败");
//        }

        //4 判断手机是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {//存在手机号
            throw new GuliException(20001,"注册失败");
        }

        //5 把数据添加数据库中
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(password));//密码加密
        member.setIsDisabled(false); //是否禁用
        member.setAvatar("无");
        baseMapper.insert(member);
    }

    //查询某一天注册人数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
